#include "taskbyproject.h"
#include "ui_taskbyproject.h"
#include "mainwindow.h"

TaskByProject::TaskByProject(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TaskByProject)
{
    ui->setupUi(this);
    this->setLayout(ui->gridLayout);

    //HaloKpy *widget =(HaloKpy *)parent; ;
    //HaloKpy *widget = static_cast<HaloKpy *>(QWidget::parentWidget());
   // MainWindow *mw = static_cast<MainWindow *>(parent);
    m_mwindow = static_cast<MainWindow *>(QWidget::parentWidget());
    m_mwindow->initComboBoxData(ui->cb_projectName,"项目名称","");

    QDate curdate = QDate::currentDate();
    ui->de_startDate->setMaximumDate(curdate);
    ui->de_startDate->setMinimumDate(curdate.addDays(-180));

    ui->de_endDate->setMinimumDate(curdate.addDays(-180));
    ui->de_endDate->setMaximumDate(curdate);

    QDate sDate=curdate.addDays(-30);
    QDate eDate =curdate;
    ui->de_startDate->setDate(sDate);
    ui->de_endDate->setDate(eDate);
    m_projectTaskGnt = new GanttModel() ;

    //设置列表view的表头
    QStringList headlist;
    headlist <<"任务名称"<<"类型"<<"预计开始"<<"截止日期"<<"完成日期"<<"状态"<<"研发人员"<<"累计耗时"<<"ID";
    m_projectTaskGnt->SetHorHeaderData(headlist);

    QVector<int> headListWidth = {350,0,80,80,80,60,60,60,50}; // 设置列宽
    m_projectTaskGnt->SetHeaderWidth(headListWidth);

    m_projectTaskGnt->SetStartEndData(sDate,eDate);

    QVBoxLayout *m_vlayout = new QVBoxLayout();
    m_vlayout->addWidget(m_projectTaskGnt);
    m_vlayout->setContentsMargins(0,0,0,0);
    ui->wd_taskProjectGnt->setLayout(m_vlayout);

    connect(m_projectTaskGnt->m_pageBar, &QPageBar::pageChanged, this, [&](int idx) {reflashPagingModelView(m_projectTaskGnt,idx);});
}

TaskByProject::~TaskByProject()
{
    delete ui;
}

void TaskByProject::reflashPagingModelView(GanttModel *prjGntModel, int curPage)
{
    prjGntModel->m_pageBar->m_currentPage = curPage;
    int modelType = prjGntModel->GetDataModelType();
    //MainWindow *mw = static_cast<MainWindow *>(QWidget::parentWidget());
    QList<GanttTask> gntTask;
    //qDebug()<<"modelType:"<<modelType;
    if(modelType==1)//根据项目名称获取项目任务
        gntTask=m_mwindow->dbMySql->getTaskDataByPrjName_new(prjGntModel->m_pageBar);
    else if(modelType==2)//根据开发人员姓名获取人员任务
        gntTask=m_mwindow->dbMySql->getTaskDataByDevName_new(prjGntModel->m_pageBar);
    else
        qDebug()<<"unknown datamodeltype:"<<modelType;

    prjGntModel->BindModelData(gntTask);
    prjGntModel->SetGanttModel();
}

void TaskByProject::on_bt_query_clicked()
{
    QString projectName = ui->cb_projectName->currentText();

    QDate showStartDate = ui->de_startDate->date();
    QDate showEndDate = ui->de_endDate->date();
    QString startDate = showStartDate.toString("yyyy-MM-dd");
    QString endDate = showEndDate.toString("yyyy-MM-dd");

    m_projectTaskGnt->m_pageBar->m_strWhere = QString(" from zt_task zt  left join zt_project zp on zp.id = zt.project  left join zt_user zu on (zu.account = zt.assignedTo or zu.account = zt.finishedBy ) left join zt_story zs on zs.id = zt.story  left join custom_role cr on cr.`key` = zu.`role` where zp.name ='%1' and zp.status = 'doing' and zt.deleted =1 and zt.closedReason !='cancel'  and ((zt.estStarted <= '%2' and zt.deadline >='%3') or (zt.estStarted <='%4' and zt.deadline >='%5') or (zt.estStarted >= '%6' and zt.deadline <='%7'))")
                                                  .arg(projectName)
                                                  .arg(startDate)
                                                  .arg(startDate)
                                                  .arg(endDate)
                                                  .arg(endDate)
                                                  .arg(startDate)
                                                  .arg(endDate);
    m_projectTaskGnt->m_pageBar->m_strCols = " zp.name,zs.title,zt.id,zt.name,zt.status,zu.realname,cr.value,zt.estStarted,zt.deadline,zt.consumed,zt.finishedDate  ";
    //MainWindow *mw = static_cast<MainWindow *>(QWidget::parentWidget());
    m_mwindow->dbMySql->getPagingInfo(m_projectTaskGnt->m_pageBar/*,m_pageBar->m_strWhere*//*,m_pageBar->m_pageRecCount*/);

    m_projectTaskGnt->m_pageBar->m_strOrd = QString(" order by zp.name,zu.realname  asc ");//limit %1,%2").arg(m_pageBar->m_pageRecCount*m_pageBar->m_currentPage).arg(m_pageBar->m_pageRecCount);

    m_projectTaskGnt->SetStartEndData(showStartDate,showEndDate);
    m_projectTaskGnt->SetDataModelType(1);
    reflashPagingModelView(m_projectTaskGnt,1);
}

void TaskByProject::on_cBox_FinishTask_clicked()
{
    qDebug()<<"on_cBox_FinishTask_clicked"<<ui->cBox_FinishTask->checkState();


}

void TaskByProject::on_cBox_FinishTask_stateChanged(int arg1)
{
    qDebug()<<"on_cBox_FinishTask_stateChanged"<<arg1;

    QString projectName = ui->cb_projectName->currentText();

    QDate showStartDate = ui->de_startDate->date();
    QDate showEndDate = ui->de_endDate->date();
    QString startDate = showStartDate.toString("yyyy-MM-dd");
    QString endDate = showEndDate.toString("yyyy-MM-dd");
    if(arg1==0)
    m_projectTaskGnt->m_pageBar->m_strWhere = QString(" from zt_task zt  left join zt_project zp on zp.id = zt.project  left join zt_user zu on (zu.account = zt.assignedTo or zu.account = zt.finishedBy ) left join zt_story zs on zs.id = zt.story  left join custom_role cr on cr.`key` = zu.`role` where zp.name ='%1' and zp.status = 'doing' and zt.deleted =1 and zt.closedReason !='cancel'  and ((zt.estStarted <= '%2' and zt.deadline >='%3') or (zt.estStarted <='%4' and zt.deadline >='%5') or (zt.estStarted >= '%6' and zt.deadline <='%7'))")
                                                  .arg(projectName)
                                                  .arg(startDate)
                                                  .arg(startDate)
                                                  .arg(endDate)
                                                  .arg(endDate)
                                                  .arg(startDate)
                                                  .arg(endDate);
    else
        m_projectTaskGnt->m_pageBar->m_strWhere = QString(" from zt_task zt  left join zt_project zp on zp.id = zt.project  left join zt_user zu on (zu.account = zt.assignedTo or zu.account = zt.finishedBy ) left join zt_story zs on zs.id = zt.story  left join custom_role cr on cr.`key` = zu.`role` where zp.name ='%1' and zp.status = 'doing' and zt.deleted =1 and zt.closedReason !='cancel'  and zt.status != 'done' and zt.status != 'closed' and ((zt.estStarted <= '%2' and zt.deadline >='%3') or (zt.estStarted <='%4' and zt.deadline >='%5') or (zt.estStarted >= '%6' and zt.deadline <='%7'))")
                                                      .arg(projectName)
                                                      .arg(startDate)
                                                      .arg(startDate)
                                                      .arg(endDate)
                                                      .arg(endDate)
                                                      .arg(startDate)
                                                      .arg(endDate);
    m_projectTaskGnt->m_pageBar->m_strCols = " zp.name,zs.title,zt.id,zt.name,zt.status,zu.realname,cr.value,zt.estStarted,zt.deadline,zt.consumed,zt.finishedDate  ";
    //MainWindow *mw = static_cast<MainWindow *>(QWidget::parentWidget());
    m_mwindow->dbMySql->getPagingInfo(m_projectTaskGnt->m_pageBar/*,m_pageBar->m_strWhere*//*,m_pageBar->m_pageRecCount*/);

    m_projectTaskGnt->m_pageBar->m_strOrd = QString(" order by zp.name,zu.realname  asc ");//limit %1,%2").arg(m_pageBar->m_pageRecCount*m_pageBar->m_currentPage).arg(m_pageBar->m_pageRecCount);

    m_projectTaskGnt->SetStartEndData(showStartDate,showEndDate);
    m_projectTaskGnt->SetDataModelType(1);
    reflashPagingModelView(m_projectTaskGnt,1);
}

